package com.tanhua.server.service;

import com.tanhua.domain.db.Question;
import com.tanhua.domain.db.Settings;
import com.tanhua.domain.db.User;
import com.tanhua.dubbo.api.db.BlackListApi;
import com.tanhua.dubbo.api.db.QuestionApi;
import com.tanhua.dubbo.api.db.SettingApi;
import com.tanhua.server.interceptors.UserHolder;
import com.tanhua.vo.PageResult;
import com.tanhua.vo.SettingsVo;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;
    @Reference
    private SettingApi settingApi;
    @Reference
    private BlackListApi blackListApi;

    public ResponseEntity querySettings() {
        User user = UserHolder.getUser();
//        构建SettingVo 数据来自tb_question和tb_settings
        Question question = questionApi.findByUserId(user.getId());
        Settings settings = settingApi.findByUserId(user.getId());

        SettingsVo settingsVo = new SettingsVo();

        settingsVo.setId(user.getId());
        settingsVo.setPhone(user.getMobile());
        if(question!=null){
            settingsVo.setStrangerQuestion(question.getTxt());
        }
        if(settings!=null){
            BeanUtils.copyProperties(settings,settingsVo);
        }

        return ResponseEntity.ok(settingsVo);


    }

    public ResponseEntity saveQuestions(String content) {
//        根据userId查询Question
        Long userId = UserHolder.getUserId();
        Question question = questionApi.findByUserId(userId);
        if(question!=null){ //   有可能是修改
            question.setTxt(content);
            questionApi.update(question);
        }else{ // 有可能是新增
            question = new Question();
            question.setTxt(content);
            question.setUserId(userId);
            questionApi.save(question);
        }
        return ResponseEntity.ok(null);
    }

   /* public ResponseEntity saveSetting(Settings settings) {
        //        根据userId查询Question
        Long userId = UserHolder.getUserId();
        Settings settingsDb = settingApi.findByUserId(userId);
        if(settingsDb!=null){ //   有可能是修改

           settings.setId(settingsDb.getId());
           settingApi.update(settings); //根据Id 有选择性的修改

        }else{ // 有可能是新增
            settings.setUserId(userId);
            settingApi.save(settings);
        }
        return ResponseEntity.ok(null);



    }*/

    public ResponseEntity saveSetting(Boolean likeNotification, Boolean pinglunNotification, Boolean gonggaoNotification) {
        Long userId = UserHolder.getUserId();
        Settings settings = settingApi.findByUserId(userId);
        if(settings!=null){
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settingApi.update(settings);
        }else{
            settings = new Settings();
            settings.setGonggaoNotification(gonggaoNotification);
            settings.setPinglunNotification(pinglunNotification);
            settings.setLikeNotification(likeNotification);
            settings.setUserId(userId);
            settingApi.save(settings);
        }
        return ResponseEntity.ok(null);
    }

    public ResponseEntity queryBlacklist(Integer page, Integer pagesize) {

        PageResult pageResult = blackListApi.queryBlacklist(page,pagesize,UserHolder.getUserId());
        return ResponseEntity.ok(pageResult);
    }

    public ResponseEntity deleteBlacklist(Long blacklistId) {
//        DELETE FROM tb_black_list WHERE black_user_id=22 AND user_id=1
        blackListApi.deleteBlacklist(blacklistId,UserHolder.getUserId());

        return ResponseEntity.ok(null);

    }
}
